from rest_framework.authentication import BaseAuthentication
from rest_framework import exceptions

from apps.login.models import UserInfo

class GeneralAuthentication(BaseAuthentication):
    # 通用认证

    def authenticate(self, request):
        token = request.META.get('HTTP_AUTHORIZATION', None)

        if not token:
            return None
        user_object = UserInfo.objects.filter(token=token).first()
        if not user_object:
            return None

        # 认证成功
        return (user_object,token)


class UserAuthentication(BaseAuthentication):
    # 用户认证，用户必须先登录

    def authenticate(self, request):
        token = request.META.get('HTTP_AUTHORIZATION', None)
        print(token)

        if not token:
            return exceptions.AuthenticationFailed()
        user_object = UserInfo.objects.filter(token=token).first()
        print(user_object)
        if not user_object:
            return exceptions.AuthenticationFailed()

        # 认证成功
        return (user_object,token)